package com.zzty.daoimpl;

import com.zzty.common.DBUtil;
import com.zzty.common.DBUtilPool;
import com.zzty.dao.ShareDao;
import com.zzty.entity.Share;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class ShareDaoImpl implements ShareDao {
    @Override
    public String insertInfo(String content, String date) {
        String result="false";
        try {
            Connection cn =DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("insert into shareInfo(content,date) values(?,?)");
            pst.setString(1, content);
            pst.setString(2, date);
            Integer count=pst.executeUpdate();
            if (count == 1) {
                result="true";
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Share getInfo() {
        Share share=new Share();
        try {
            Connection cn =DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("select * from shareInfo");
            ResultSet rs=pst.executeQuery();
            while (rs.next()) {
                share.setId(rs.getInt("Id"));
                share.setDate(rs.getString("date"));
                share.setContent(rs.getString("content"));
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return share == null ? null : share;
    }

    public String checkDate(String date) {
        String result="true";
        try {
            Connection cn =DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("select date from shareInfo");
            ResultSet rs=pst.executeQuery();
            while (rs.next()) {
                if (date.equals(rs.getString("date"))) {
                    result="false";
                }
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String update(String content,String date) {
        String result = "false";
        try {
            Connection cn =DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("update shareInfo set content=? where date =?");
            pst.setString(1, content);
            pst.setString(2, date);
            Integer count=pst.executeUpdate();
            if (count == 1) {
                result="true";
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public List getShare() {
        List<Share>shares=null;
        try {
            Connection cn =DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("select * from shareInfo");
            ResultSet rs=pst.executeQuery();
            while (rs.next()) {
                if(shares==null){
                    shares = new ArrayList<>();
                }
                Share share = new Share();
                share.setId(rs.getInt("Id"));
                share.setDate(rs.getString("date"));
                share.setContent(rs.getString("content"));
                shares.add(share);
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return shares;

    }

    @Override
    public String searchToday(String date) {
        Share share = new Share();
        try {
            Connection cn =DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("select content from shareInfo where date =?");
            pst.setString(1,date);
            ResultSet rs=pst.executeQuery();
            while (rs.next()) {
                share.setContent(rs.getString("content"));
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return share.getContent();
    }
}